<template>
  <el-button v-copy:[copyEventName]="msg">点我一键复制</el-button>
  <el-input v-model="msg"></el-input>
</template>
<script setup lang="ts">
import { ref } from 'vue'

let msg = ref('hello wolrd')
const copyEventName = (value) => {
  console.log('copyEventName')
  console.log(value)
}
const vCopy = {
  beforeMount(el, binding) {
    el.copyValue = binding.value

    el.addEventListener('click', () => {
      console.log('click')
      const textarea = document.createElement('textarea')
      textarea.style.position = 'fixed'
      textarea.style.left = '1000px'

      textarea.readOnly = true
      textarea.value = el.copyValue
      document.body.appendChild(textarea)
      textarea.select()
      const result = document.execCommand('copy')
      if (result) {
        console.log('复制成功')
      }
      document.body.removeChild(textarea)
    })
  },
  updated(el, binding) {
    el.copyValue = binding.value
  },
  unmounted(el) {
    el.removeEventListener('click', () => {})
  }
}
</script>
<style lang="scss" scoped></style>
